package com.hanson.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanson.common.bean.ResultMessage;
import com.hanson.common.util.PageInfo;
import com.hanson.entity.GoodsRuleBean;
import com.hanson.entity.GroupGoodsBean;
import com.hanson.entity.SeckillGoodsBean;
import com.hanson.mapper.UserBeanMapper;
import com.hanson.service.GoodsRuleService;
import com.hanson.service.SeckillGoodsService;
import com.hanson.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/admin/seckillGoods")
public class SeckillGoodsController {
    @Autowired
    private SeckillGoodsService seckillGoodsService;
    @Autowired
    private GoodsRuleService goodsRuleService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String login(ModelMap modelMap) {
        return "admin/seckillGoods/index";
    }

    @RequestMapping(value = "/modify", method = RequestMethod.GET)
    public String modify(ModelMap modelMap, String id) {
        if (StringUtils.isNotEmpty(id)) {
            modelMap.put("data", seckillGoodsService.selectById(Integer.parseInt(id)));
        }
        return "admin/seckillGoods/modify";
    }

    @RequestMapping(value = "listData", method = RequestMethod.GET)
    @ResponseBody
    public PageInfo<SeckillGoodsBean> listData(int limit, int page, SeckillGoodsBean seckillGoodsBean) {
        System.out.println(seckillGoodsBean);
        Page<SeckillGoodsBean> pageInfo = new Page<>(page, limit);
        return new PageInfo(seckillGoodsService.listSeckillGoods(pageInfo, seckillGoodsBean));
    }

    @RequestMapping(value = "seckillRule", method = RequestMethod.GET)
    public String seckillRule(ModelMap modelMap, String id) {
        modelMap.put("id", id);
        modelMap.put("data", JSON.toJSONString(seckillGoodsService.getRules(Integer.parseInt(id))));
        List<GoodsRuleBean> goodsRuleBeanList = goodsRuleService.getAllRule();
        if (CollectionUtils.isNotEmpty(goodsRuleBeanList)) {
            modelMap.put("ruleList", JSON.toJSONString(goodsRuleBeanList));
        }
        return "admin/seckillGoods/seckillRule";
    }

    @RequestMapping(value = "setRule", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage setRule(@RequestBody SeckillGoodsBean seckillGoodsBean) {
        ResultMessage resultMessage = new ResultMessage();
        seckillGoodsService.setRules(seckillGoodsBean);
        resultMessage.setMsg("设置秒杀规格成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage add(SeckillGoodsBean seckillGoodsBean, HttpServletRequest request) throws Exception {
        String[] times = request.getParameterValues("selectTime");
        System.out.println(Arrays.toString(times));
        ResultMessage resultMessage = new ResultMessage();
        seckillGoodsService.add(seckillGoodsBean);
        resultMessage.setMsg("新增秒杀信息成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage modifyData(SeckillGoodsBean seckillGoodsBean) throws Exception {
        ResultMessage resultMessage = new ResultMessage();
        seckillGoodsService.modify(seckillGoodsBean);
        resultMessage.setMsg("修改秒杀信息成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage delete(String id) {
        ResultMessage resultMessage = new ResultMessage();
        seckillGoodsService.deleteById(Integer.parseInt(id));
        resultMessage.setMsg("删除秒杀成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/updateIsShow/{isShow}/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResultMessage updateIsShow(@PathVariable String isShow, @PathVariable  String id) {
        ResultMessage resultMessage = new ResultMessage();
        seckillGoodsService.updateIsShow(Integer.parseInt(isShow),Integer.parseInt(id));
        resultMessage.setMsg("修改状态成功！");
        return resultMessage;
    }
}
